package com.qiantu.phone.ui.fragment.device;

import android.view.View;

import androidx.lifecycle.LifecycleOwner;

import com.hjq.http.listener.HttpCallback;
import com.hjq.toast.ToastUtils;
import com.qiantu.api.db.LLDeviceDBManager;
import com.qiantu.api.entity.DeviceCustomKeyBean;
import com.qiantu.api.http.LLHttpManager;
import com.qiantu.api.http.model.HttpData;
import com.qiantu.phone.R;
import com.qiantu.phone.app.AppActivity;
import com.qiantu.phone.app.AppApplication;
import com.qiantu.phone.event.DeviceItemChangeEvent;
import com.qiantu.phone.ui.uiutil.DeviceType;
import com.qiantu.phone.ui.uiutil.LLDeviceUtil;
import com.qiantu.phone.widget.btnview.FiveButtonView;
import com.qiantu.phone.widget.btnview.OnBtnClickListener;
import com.qiantu.phone.widget.btnview.OneButtonView;
import com.qiantu.phone.widget.btnview.OneTwoThreeFourSixEightNineButtonView;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import okhttp3.Call;

/**
 * desc   : 4键
 *  红外键数
 */
public final class WirelessInfraredFragment extends KeyDeviceFragment {

    private OneButtonView btnOneView;
    private OneTwoThreeFourSixEightNineButtonView btnTwoView;
    private OneTwoThreeFourSixEightNineButtonView btnThreeView;
    private OneButtonView btnSevenView1;
    private OneButtonView btnSevenView2;
    private FiveButtonView btnSevenView5;
    private OneButtonView btnTenView1;
    private OneTwoThreeFourSixEightNineButtonView btnTenView9;
    private String key;

    public static WirelessInfraredFragment newInstance() {
        WirelessInfraredFragment fragment = new WirelessInfraredFragment();
//        Bundle bundle = new Bundle();
//        bundle.putString("deviceType", deviceType);
//        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_wreless_infrared;
    }

    @Override
    protected void initView() {
        super.initView();
        btnOneView = findViewById(R.id.btn_one_view);
        btnTwoView = findViewById(R.id.btn_two_view);
        btnThreeView = findViewById(R.id.btn_three_view);
        btnSevenView1 = findViewById(R.id.btn_seven_view1);
        btnSevenView2 = findViewById(R.id.btn_seven_view2);
        btnSevenView5 = findViewById(R.id.btn_seven_view5);
        btnTenView1 = findViewById(R.id.btn_ten_view1);
        btnTenView9 = findViewById(R.id.btn_ten_view9);

        //1键
        btnOneView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                operation(1);
            }
        });

        //2键
        btnTwoView.setOnBtnClickListener(new OnBtnClickListener() {
            @Override
            public void onClick(int btnID) {
                operation(btnID);
            }
        });

        //3键
        btnThreeView.setOnBtnClickListener(new OnBtnClickListener() {
            @Override
            public void onClick(int btnID) {
                operation(btnID);
            }
        });

        //7键
        btnSevenView1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                operation(1);
            }
        });
        btnSevenView2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                operation(2);
            }
        });
        btnSevenView5.setOnBtnClickListener(new OnBtnClickListener() {
            @Override
            public void onClick(int btnID) {
                int value = btnID + 2;
                operation(value);
            }
        });

        //10键
        btnTenView1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                operation(1);
            }
        });
        btnTenView9.setOnBtnClickListener(new OnBtnClickListener() {
            @Override
            public void onClick(int btnID) {
                int value = btnID + 1;
                operation(value);
            }
        });
        updateView();
    }

    private void operation(int value) {
        Map<String, Object> option = new HashMap<>();
        option.put(key, value);
        Map<String, Object> status = new HashMap<>();
        status.put(STATUS, option);
        LLHttpManager.operationDevice((LifecycleOwner) getContext(), mDevice.getDeviceSerialNo(), status, new HttpCallback<HttpData<Void>>(null) {
            @Override
            public void onStart(Call call) {
                if(getContext() != null && getContext() instanceof AppActivity) {
                    ((AppActivity) getContext()).showDialog("执行中...");
                }
            }

            @Override
            public void onSucceed(HttpData<Void> result) {
            }

            @Override
            public void onFail(Exception e) {
                ToastUtils.show(R.string.exec_failure);
            }

            @Override
            public void onEnd(Call call) {
                if(getContext() != null && getContext() instanceof AppActivity) {
                    ((AppActivity) getContext()).hideDialog();
                }
            }
        });
    }

//    private void getDeviceCustomKeyList() {
//        LLHttpManager.getDeviceCustomKeyList(this, mDevice.getDeviceSerialNo(), new HttpCallback<HttpData<DeviceCustomKeyBean>>(null) {
//            @Override
//            public void onStart(Call call) {
//
//            }
//
//            @Override
//            public void onSucceed(HttpData<DeviceCustomKeyBean> result) {
//                List<DeviceCustomKeyBean.KeyData> customKeyList = result.getData().getDeviceCustomKeys();
//                updateCustomKey(customKeyList);
//                mDevice.setDeviceCustomKeys(customKeyList);
//                LLDeviceDBManager.getInstance(getContext()).put(mDevice);
//            }
//
//            @Override
//            public void onFail(Exception e) {
//
//            }
//
//            @Override
//            public void onEnd(Call call) {
//            }
//        });
//    }

    public void updateCustomKey(List<DeviceCustomKeyBean> customKeyList) {
        String defaultName = AppApplication.getInstance().getResources().getString(R.string.custom_name);
        String key1 = defaultName;
        String key2 = defaultName;
        String key3 = defaultName;
        String key4 = defaultName;
        String key5 = defaultName;
        String key6 = defaultName;
        String key7 = defaultName;
        String key8 = defaultName;
        String key9 = defaultName;
        String key10 = defaultName;
        if (customKeyList != null) {
            for (DeviceCustomKeyBean keyData : customKeyList) {
                switch (keyData.getKeyIndex()) {
                    case "1":
                        key1 = keyData.getKeyName();
                        break;
                    case "2":
                        key2 = keyData.getKeyName();
                        break;
                    case "3":
                        key3 = keyData.getKeyName();
                        break;
                    case "4":
                        key4 = keyData.getKeyName();
                        break;
                    case "5":
                        key5 = keyData.getKeyName();
                        break;
                    case "6":
                        key6 = keyData.getKeyName();
                        break;
                    case "7":
                        key7 = keyData.getKeyName();
                        break;
                    case "8":
                        key8 = keyData.getKeyName();
                        break;
                    case "9":
                        key9 = keyData.getKeyName();
                        break;
                    case "10":
                        key10 = keyData.getKeyName();
                        break;
                }
            }
        }
        btnThreeView.setText(key1, key2, key3);
        btnTenView9.setText(key2, key3, key4, key5, key6, key7, key8, key9, key10);
    }

    protected void updateView() {
        super.updateView();
        updateCustomKey(mDevice.getDeviceCustomKeys());
        updateByDeviceType();
    }

    private void updateByDeviceType() {
        String deviceType = mDevice.getDeviceType();
        if (deviceType.contains("wireless_433")) {
            key = LLDeviceUtil.WIRELESS_CTRL_433;
        } else if (deviceType.contains("wireless_315")) {
            key = LLDeviceUtil.WIRELESS_CTRL_315;
        } else if (deviceType.contains("infra-red")) {
            key = LLDeviceUtil.IR_CTRL;
        }
        if (deviceType.equals(DeviceType.WIRELESS_433_KEY_1.stringType)
                || deviceType.equals(DeviceType.WIRELESS_315_KEY_1.stringType)
                || deviceType.equals(DeviceType.INFRARED_KEY_1.stringType)) {
            btnOneView.setVisibility(View.VISIBLE);
            btnTwoView.setVisibility(View.GONE);
            btnThreeView.setVisibility(View.GONE);
            btnSevenView1.setVisibility(View.GONE);
            btnSevenView2.setVisibility(View.GONE);
            btnSevenView5.setVisibility(View.GONE);
            btnTenView1.setVisibility(View.GONE);
            btnTenView9.setVisibility(View.GONE);
        } else if (deviceType.equals(DeviceType.WIRELESS_433_KEY_2.stringType)
                || deviceType.equals(DeviceType.WIRELESS_315_KEY_2.stringType)
                || deviceType.equals(DeviceType.INFRARED_KEY_2.stringType)) {
            btnOneView.setVisibility(View.GONE);
            btnTwoView.setVisibility(View.VISIBLE);
            btnThreeView.setVisibility(View.GONE);
            btnSevenView1.setVisibility(View.GONE);
            btnSevenView2.setVisibility(View.GONE);
            btnSevenView5.setVisibility(View.GONE);
            btnTenView1.setVisibility(View.GONE);
            btnTenView9.setVisibility(View.GONE);
        } else if (deviceType.equals(DeviceType.WIRELESS_433_KEY_3.stringType)
                || deviceType.equals(DeviceType.WIRELESS_315_KEY_3.stringType)
                || deviceType.equals(DeviceType.INFRARED_KEY_3.stringType)) {
            btnOneView.setVisibility(View.GONE);
            btnTwoView.setVisibility(View.GONE);
            btnThreeView.setVisibility(View.VISIBLE);
            btnSevenView1.setVisibility(View.GONE);
            btnSevenView2.setVisibility(View.GONE);
            btnSevenView5.setVisibility(View.GONE);
            btnTenView1.setVisibility(View.GONE);
            btnTenView9.setVisibility(View.GONE);
            getDeviceCustomKeyList();
        } else if (deviceType.equals(DeviceType.WIRELESS_433_KEY_7.stringType)
                || deviceType.equals(DeviceType.WIRELESS_315_KEY_7.stringType)
                || deviceType.equals(DeviceType.INFRARED_KEY_7.stringType)) {
            btnOneView.setVisibility(View.GONE);
            btnTwoView.setVisibility(View.GONE);
            btnThreeView.setVisibility(View.GONE);
            btnSevenView1.setVisibility(View.VISIBLE);
            btnSevenView2.setVisibility(View.VISIBLE);
            btnSevenView5.setVisibility(View.VISIBLE);
            btnTenView1.setVisibility(View.GONE);
            btnTenView9.setVisibility(View.GONE);
        } else if (deviceType.equals(DeviceType.WIRELESS_433_KEY_9.stringType)
                || deviceType.equals(DeviceType.WIRELESS_315_KEY_9.stringType)
                || deviceType.equals(DeviceType.INFRARED_KEY_9.stringType)) {
            btnOneView.setVisibility(View.GONE);
            btnTwoView.setVisibility(View.GONE);
            btnThreeView.setVisibility(View.GONE);
            btnSevenView1.setVisibility(View.GONE);
            btnSevenView2.setVisibility(View.GONE);
            btnSevenView5.setVisibility(View.GONE);
            btnTenView1.setVisibility(View.VISIBLE);
            btnTenView9.setVisibility(View.VISIBLE);
            getDeviceCustomKeyList();
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onDeviceChangeEvent(DeviceItemChangeEvent event) {
        mDevice = LLDeviceDBManager.getInstance(getContext()).getDeviceBySerialNo(event.deviceSerialNo);
        updateView();

    }

}